package edu.nctu.csie.jichang.database.model;

import java.sql.Clob;
import java.sql.Timestamp;
import java.sql.Types;

import edu.nctu.csie.jichang.database.model.cell.ColumnType;
import edu.nctu.csie.jichang.database.model.datavalue.DBBoolean;
import edu.nctu.csie.jichang.database.model.datavalue.DBDate;
import edu.nctu.csie.jichang.database.model.datavalue.DBInteger;
import edu.nctu.csie.jichang.database.model.datavalue.DBObject;
import edu.nctu.csie.jichang.database.model.datavalue.DBString;
import edu.nctu.csie.jichang.database.model.datavalue.DataValue;
import edu.nctu.csie.jichang.database.util.DBException;
import edu.nctu.csie.jichang.database.util.StringUtil;

public class Converter {

	private Converter(){
	}

	public static ColumnType getColumnType(int pJDBCType){
		switch(pJDBCType){
			case Types.CHAR:
			case Types.VARCHAR:
			case Types.NVARCHAR:
			case Types.LONGNVARCHAR:
			case Types.CLOB:
				return ColumnType.String;
			case Types.NUMERIC:
			case Types.DECIMAL:
			case Types.INTEGER:
			case Types.DOUBLE:
			case Types.REAL:
				return ColumnType.Integer;
			case Types.BIT:
				return ColumnType.Boolean;
			case Types.DATE:
			case Types.TIMESTAMP:
				return ColumnType.Date;
			case Types.BINARY:
				return ColumnType.Binary;
		}
		throw new DBException("Unknow Type " + pJDBCType);
	}
//	
//	public static ColumnType getColumnType(String pType){
//		if(pType.equalsIgnoreCase("String")){
//			return ColumnType.String;
//		}else if(pType.equalsIgnoreCase("Integer")){
//			return ColumnType.Integer;
//		}else if(pType.equalsIgnoreCase("Date")){
//			return ColumnType.Date;
//		}else if(pType.equalsIgnoreCase("Binary")){
//			return ColumnType.Binary;
//		}else if(pType.equalsIgnoreCase("Boolean")){
//			return ColumnType.Boolean;
//		}
//		throw new IENException("Unknow Type " + pType);
//	}
	
	public static DataValue<?> getDataType(int pJDBCType,Object pObject){
		switch(pJDBCType){
			case Types.VARCHAR:
			case Types.CHAR:
			case Types.CLOB:
				return new DBString( pObject );
			case Types.NUMERIC:
			case Types.DECIMAL:
			case Types.INTEGER:
			case Types.SMALLINT:
			case Types.DOUBLE:
				return new DBInteger( pObject );
			case Types.DATE:
			case Types.TIMESTAMP:
				return new DBDate( pObject );
			case Types.BIT:
				return new DBBoolean( pObject );
			case Types.BINARY:
				return new DBObject( pObject );
		}
		throw new DBException("unknow type = " + pJDBCType);
	}

	public static String doObjectToString(Object pValue) {

		if (pValue == null) return null;

		if (pValue instanceof Integer || pValue instanceof Boolean || pValue instanceof Double  ) return String.valueOf(pValue);

		if (pValue instanceof Timestamp) return ((Timestamp) pValue).toString();

		if (pValue instanceof String) return (String)pValue;

		if (pValue instanceof Clob) return StringUtil.getString(pValue);

		throw new DBException("不支援的型態 = " + pValue.getClass().getSimpleName());
	}
}
